define(['template/mobile/edit','mobile/style','mobile/ani','wx','wx.tpl','wx.ajax'],function(tpl,styleCon,aniCon,wx){
	var data;
	var entity = {};
	var id;

	entity.save = function(id){
		data = [];
		//$('#Js-container').find('li').each(function(){
		$('#Js-pageList').find('li').each(function(){
			var pageid = $(this).find('.Js-pageList-del').attr('data-id');
			var pageContent = $('#Js-container').find('#page'+pageid);
			var pageData = {bg:pageContent.css('background-image')||'',css:[],other:pageContent.data('other')};
			pageContent.find('.mobile-comppont').each(function(){
				var compont = $(this).data('info');
				if(compont.ani.length > 0){
					/*$(this).css('animation','');
					var anistr = '';
					if(compont.ani.length  == 1){
						anistr += compont.ani[0].aniname + ' ' + compont.ani[0].durtime + 's '+compont.ani[0].anitimfn+' ' + compont.ani[0].delaytime + 's '+'both '+compont.ani[0].lptimes+','
					}else{
						for(var i=0;i<compont.ani.length;i++){
							anistr += compont.ani[i].aniname + ' ' + compont.ani[i].durtime + 's '+compont.ani[i].anitimfn+' ' + compont.ani[i].delaytime + 's '+'forwards '+compont.ani[i].lptimes+','
						}
					}
					$(this).css('animation',anistr);*/
					$(this).hide().css('opacity',1);
				}
				compont.html = $(this)[0].outerHTML;
				compont.top = $(this).parent().css('top');
				compont.left = $(this).parent().css('left');
				compont.width = $(this).parent().width();
				compont.height = $(this).parent().height();
				pageData.css.push(compont);
				if(compont.ani.length > 0){
					/*$(this).css('animation','');
					var anistr = '';
					for(var i=0;i<compont.ani.length;i++){
						anistr += compont.ani[i].aniname + ' ' + compont.ani[i].durtime + 's '+compont.ani[i].anitimfn+' ' + compont.ani[i].delaytime + 's '+compont.ani[i].lptimes+','
					}
					$(this).css('animation',anistr);*/
					$(this).show();
				}
			});
			data.push(pageData);
		});
		var setting = $('#Js-container').data('setting');
		wx.sendData('/mobileh5/save.action?_id='+id,{type:'POST',contentType:'application/json',data:JSON.stringify({'content':data,'setting':setting})},function(data){
			if(data.status == '0')
				$.succ('保存成功');
			else
				$.fail('保存失败');
		});
	};

	entity.configSetting = function(id,addPage,callbak){
		// TODO 查询活动详情
		wx.sendData('/mobileh5/detail.action?_id='+id,{type:'get'},function(data){
			var actInfo = {};
			actInfo.description = data.result.description;
			actInfo.name = data.result.name;
			actInfo.id = data.result._id;
			actInfo.is_del = data.result.is_del;
			data = eval('(' + data.result.content + ')');
			$('#Js-container').find('li:eq(0)').removeClass('f-hide');
			if(data == null) {
				callbak(actInfo);
				return;
			}
			for(var i = 0;i<data.content.length;i++){
				var currentPage = $('#page'+(i+1));
				if(currentPage.length == 0){
					addPage();
					currentPage = $('#page'+(i+1));
				}
				currentPage.css('background-image',data.content[i].bg);
				if(!data.content[i].css) continue;
				for(var j = 0;j<data.content[i].css.length;j++){
					var $compont,item = data.content[i].css[j];
					if(item.type == '0'){
						$compont = $('#Js-addText').triggerHandler('click',[item.html,currentPage]);
					}else if(item.type == '1'){
						$compont = $('#Js-upload').triggerHandler('upload',[item.html,currentPage]);
					} else if(item.type == '2'){
						$compont = $('#Js-addInput').triggerHandler('click',[item.html,currentPage]);
					} else if(item.type == '3'){
						$compont = $('#Js-addSubBtn').triggerHandler('click',[item.html,currentPage]);
					}
					$compont.find('.mobile-comppont').data('info',item).show();
					$compont.css({top:item.top,left:item.left,width:item.width,height:item.height});
					$compont.find('.Js-comp-resize').hide();
				}
			}
			$('#Js-container').data('setting', data);
			callbak(actInfo);
		})
	};

	return entity;
});